package com.vonchange.utils.session;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vonchange.common.cookie.CookieUtil;
import com.vonchange.utils.config.SystemConfig;

public class SessionUtil {
	public static final String USER_SESSION_KEY ="login_user";
	public static final String MSG_ERROR = "msg_error"; 
	public static final String MSG_INFO = "msg_info"; 
	@SuppressWarnings("unchecked")
	public static <T>T getSession(HttpServletRequest request,String key){
		Object obj=request.getSession().getAttribute(key);
		if(null==obj){
			return null;
		}
		return (T) obj;
	}
	public static  void setError(HttpServletRequest request, String msg){
		   SessionUtil.setSesssion(request, SessionUtil.MSG_ERROR, msg);
	}
	public static  void setInfo(HttpServletRequest request, String msg){
		   SessionUtil.setSesssion(request, SessionUtil.MSG_INFO, msg);
	}
    public static  void setSesssion(HttpServletRequest request,String key,Object obj){
    	request.getSession().setAttribute(key, obj);
			// session 30 分钟失效
		int sessionExpireTime = SystemConfig.getIntProperty("session.expire.time", 30 * 60);
		request.getSession().setMaxInactiveInterval(sessionExpireTime);
     }
 
     public static void clearUser(HttpServletRequest request,HttpServletResponse response){
    	 request.getSession().removeAttribute(USER_SESSION_KEY);
    	 CookieUtil.clearUserCookie(response);
     }

}
